// models/studentInfo.js
const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const StudentInfo = sequelize.define('student', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING, allowNull: false },
  student_no: { type: DataTypes.STRING, allowNull: false, unique: true },
  gender: { type: DataTypes.STRING, defaultValue: '保密' },
  id_card: { type: DataTypes.STRING, allowNull: false, unique: true },
  password: { type: DataTypes.STRING, allowNull: false },
  points: { type: DataTypes.INTEGER, defaultValue: 0 }
}, {
  tableName: 'students',
  timestamps: false
});

// 获取所有学生信息的方法
StudentInfo.getAllStudents = async function() {
  try {
    const students = await StudentInfo.findAll();  // 查询所有学生数据
    return students;
  } catch (error) {
    console.error('Error fetching students:', error);
    throw error;
  }
};

// 根据id获取学生信息的方法
StudentInfo.getStudentById = async function(id) {
  try {
    const student = await StudentInfo.findByPk(id);
    if (!student) {
      throw new Error('未找到该学生');
    }
    return student;
  } catch (error) {
    console.error('Error fetching student by id:', error);
    throw error;
  }
};

module.exports = StudentInfo;
